package com.tbit.uqbike.service.rpcservice.impl;

import com.tbit.uqbike.constant.MapConstant;
import com.tbit.uqbike.object.business.Point;
import com.tbit.uqbike.object.pojo.MachineFault;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.base.CalibrateService;
import com.tbit.uqbike.service.rpcservice.MachineFaultService;
import com.tbit.uqbike.webmanager.dao.core.MachineFaultDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 车辆故障
 *
 * @author Leon
 * 2019年11月16日 下午2:56:53
 */
@Service("machineFaultService")
public class MachineFaultServiceImpl implements MachineFaultService {
    @Autowired
    private MachineFaultDao machineFaultDao;
    @Autowired
    private CalibrateService calibrateService;

    @Override
    public MachineFault getByMachineId(LoginInfo info, Integer machineId, Integer mapType) {
        MachineFault machineFault = machineFaultDao.getByMachineId(machineId);
        if (machineFault != null && machineFault.getPoint() != null && !"".equals(machineFault.getPoint())) {
            String[] strs = machineFault.getPoint().split(",");
            Point point = new Point(Double.parseDouble(strs[0]), Double.parseDouble(strs[1]));
            point = calibrateService.commonCalibrate(point, MapConstant.MAP_ORI, mapType);
            machineFault.setLat(point.getY());
            machineFault.setLon(point.getX());
        }
        if (machineFault != null) {
            machineFault.parseFaults();
        }
        return machineFault;
    }

    @Override
    public void insert(MachineFault machineFault) {
        machineFault.setUpdateTime(new Date());
        machineFaultDao.insert(machineFault);
    }

    @Override
    public void update(MachineFault machineFault) {
        machineFault.setUpdateTime(new Date());
        machineFaultDao.update(machineFault);
    }

    @Override
    public void del(Integer machineId) {
        machineFaultDao.del(machineId);
    }

    @Override
    public List<MachineFault> getByAccountId(Integer accountId, Integer mapType) {
        List<MachineFault> list = machineFaultDao.getByAccountId(accountId);
        if (list.size() > 0) {
            list.stream().forEach(machineFault -> {
                String[] strings = machineFault.getPoint().split(",");
                Point point = new Point(Double.parseDouble(strings[0]), Double.parseDouble(strings[1]));
                point = calibrateService.commonCalibrate(point, mapType, MapConstant.MAP_ORI);
                machineFault.setLon(point.getX());
                machineFault.setLat(point.getY());
            });
        }
        return list;
    }

}
